<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
		"http://www.mybatis.org/dtd/mybatis-3-mapper.dtd">

<mapper namespace="${Configuration.packageProject}.${Configuration.packageDao}.${Configuration.daoClass}">
	<!-- 通用查询结果列-->
	<#if columnEntity?exists>
	<sql id="baseColumnList">
		<#list columnEntity as data>
			<#if ((data_index + 1)==(columnEntity?size))>
			${Configuration.tableAlias}.${data.column} as ${data.variable}
			<#else>
			${Configuration.tableAlias}.${data.column} as ${data.variable},
			</#if>
		</#list>
	</sql>
	</#if>
	
	<!-- 模糊查询 -->
	<#if columnEntity?exists>
	<sql id="fuzzy_query_obj">
		<if test = "o != null">
		<#list columnEntity as data>
			<#if data.type=="String">
			<if test="o.${data.variable} != null and o.${data.variable} != ''">
				and ${Configuration.tableAlias}.${data.column} like lower(concat('%',<#noparse>#{</#noparse>o.${data.variable}},'%'))
			</if>
			<#else>
			<if test="o.${data.variable} != null">
				and ${Configuration.tableAlias}.${data.column} like lower(concat('%',<#noparse>#{</#noparse>o.${data.variable}},'%'))
			</if>	
			</#if>
		</#list>
		</if>
	</sql>
	</#if>

	<#if columnEntity?exists>
	<sql id="fuzzy_query">
		<#list columnEntity as data>
			<#if data.type=="String">
			<if test="${data.variable} != null and ${data.variable} != ''">
				and ${Configuration.tableAlias}.${data.column} like lower(concat('%',<#noparse>#{</#noparse>${data.variable}},'%'))
			</if>
			<#else>
			<if test="${data.variable} != null">
				and ${Configuration.tableAlias}.${data.column} like lower(concat('%',<#noparse>#{</#noparse>${data.variable}},'%'))
			</if>	
			</#if>
		</#list>
	</sql>
	</#if>
	
	<#if columnEntity?exists>
	<!-- 全字符匹配查询 条件 -->
	<sql id="query_obj">
		<if test = "o != null">
		<#list columnEntity as data>
			<#if data.type=="String">
			<if test="o.${data.variable} != null and o.${data.variable} != ''">
				and ${Configuration.tableAlias}.${data.column} = <#noparse>#{</#noparse>o.${data.variable}}
			</if>
			<#else>
			<if test="o.${data.variable} != null">
				and ${Configuration.tableAlias}.${data.column} = <#noparse>#{</#noparse>o.${data.variable}}
			</if>	
			</#if>
		</#list>
		</if>
	</sql>
	</#if>
	
	<#if columnEntity?exists>
	<sql id="query">
		<#list columnEntity as data>
			<#if data.type=="String">
			<if test="${data.variable} != null and ${data.variable} != ''">
				and ${Configuration.tableAlias}.${data.column} = <#noparse>#{</#noparse>${data.variable}}
			</if>
			<#else>
			<if test="${data.variable} != null">
				and ${Configuration.tableAlias}.${data.column} = <#noparse>#{</#noparse>${data.variable}}
			</if>	
			</#if>
		</#list>
	</sql>
	</#if>
		
	<select id="getPage" resultType = "${Configuration.entityClass}">
		select 
		<include refid="baseColumnList"/>
		from ${Configuration.tableName} ${Configuration.tableAlias}
		where 1 = 1
		<include refid="fuzzy_query_obj"></include>
		<if test="orderBy != null">
			order by <#noparse>${</#noparse>orderBy}
			<choose>
				<when test="sort !=null"><#noparse>${</#noparse>sort}</when>
				<otherwise>desc</otherwise>
			</choose>
		</if>
		<if test="pageStart != null and pageSize != null">
			limit <#noparse>#{</#noparse>pageStart},<#noparse>#{</#noparse>pageSize}		
		</if>
		
	</select>
	
	<select id="getCount" parameterType="${Configuration.entityClass}" resultType = "int">
		select 
		count(1)
		from ${Configuration.tableName} ${Configuration.tableAlias}
		where 1 = 1
		<include refid="fuzzy_query"></include>
	</select>
	
	<!-- 根绝条件获取一个 -->
	<select id="selectOne" parameterType="${Configuration.entityClass}" resultType="${Configuration.entityClass}">
		select 
		<include refid="baseColumnList"/>
		from ${Configuration.tableName} ${Configuration.tableAlias}
		where 1 = 1
		<include refid="query"/>
		limit 1
	</select>
	
	<select id="selectList" parameterType="${Configuration.entityClass}" resultType="${Configuration.entityClass}">
		select 
		<include refid="baseColumnList"/>
		from ${Configuration.tableName} ${Configuration.tableAlias}
		where 1 = 1
		<include refid="query"/>
	</select>
	
	<#if columnEntity?exists>
	<!-- 根据id更新 -->
	<update id="updateById" parameterType="${Configuration.entityClass}">
		update ${Configuration.tableName}
		<set>
			<#list columnEntity as data>
			<#if data.type=="String">
			<if test="${data.variable} != null and ${data.variable} != ''">
				${data.column} = <#noparse>#{</#noparse>${data.variable}},
			</if>
			<#else>
			<if test="${data.variable} != null">
				${data.column} = <#noparse>#{</#noparse>${data.variable}},
			</if>	
			</#if>
			</#list>
		</set>
		where id = <#noparse>#{</#noparse>id}
	</update>
	</#if>
	
	<!-- 根据条件删除 -->
	<delete id="delete" parameterType="${Configuration.entityClass}">
		update ${Configuration.tableName} ${Configuration.tableAlias}
		set del = "1"
		where 1 = 1
		<include refid="query"/>
	</delete>

	<delete id="deletes" parameterType="list">
		update ${Configuration.tableName}
		set del = "1"
		where 1 = 1
		and id in
		(
		<foreach collection='list' item='item' index='index' separator=','>
			<#noparse>#{</#noparse>item.id}
		</foreach>
		)
	</delete>

	<!-- 插入 -->
	<insert id="insert" parameterType="${Configuration.entityClass}">
		insert into ${Configuration.tableName}
		(
		<#list columnEntity as data>
			<#if ((data_index + 1) == (columnEntity?size))>
			${data.column}
			<#else>
			${data.column},
			</#if>
		</#list>	
		)
		values 
		(
		<#list columnEntity as data>
			<#if ((data_index + 1) == (columnEntity?size))>
			<#noparse>#{</#noparse>${data.variable}}
			<#else>
			<#noparse>#{</#noparse>${data.variable}},
			</#if>
		</#list>	
		)
	</insert>
	
	<!-- 批量插入 -->
	<insert id="insertBatch" parameterType="list">
		insert into ${Configuration.tableName}
		(
		<#list columnEntity as data>
			<#if ((data_index + 1) == (columnEntity?size))>
			${data.column}
			<#else>
			${data.column},
			</#if>
		</#list>	
		)
		values
		<foreach collection='list' item='item' index='index' separator=','>
		(
		<#list columnEntity as data>
			<#if ((data_index + 1) == (columnEntity?size))>
			<#noparse>#{</#noparse>item.${data.variable}}
			<#else>
			<#noparse>#{</#noparse>item.${data.variable}},
			</#if>
		</#list>	
		)
		</foreach>
	</insert>
</mapper>